/**
 * @author helio
 */
if (!SALE.window) SALE.window = {}

if (!SALE.window.ProductsEditWindow) {
	SALE.window.ProductsEditWindow = Ext.extend(SALE.basicWindow, {
		
		title: '商品添加/编辑',
		closable: true,
		collapsible: true,
		width: 600,
		height: 425,
		plain: true,
		closeAction :'hide',
		layout: 'fit',
		initComponent: function(){
			this.inputForm = new Ext.FormPanel({
				//frame: true,
				bodyStyle : 'background-color: transparent; padding:5px;',
				border: false,
				labelWidth: 55,
				defaultType: 'textfield',
				
				items: [{
		            xtype:'fieldset',
					title: '基本信息',
					autoHeight:true,
		            anchor:'100%',
					layout:'column',
					
					items: [{
						columnWidth:.7,
						layout: 'form',
		                border:false,
						bodyStyle : 'background-color: transparent;',
						items: [{
							xtype:'textfield',
							fieldLabel: '商品名称',
	                		name: 'name',
	                		anchor:'95%'
						},{
		                    xtype:'textfield',
		                    fieldLabel: '商品简称',
		                    name: 'abbr',
		                    anchor:'70%'
		                },{
		                    xtype:'textfield',
		                    fieldLabel: '商品编号',
		                    name: 'code',
		                    anchor:'70%'
		                },{
		                    xtype:'textfield',
		                    fieldLabel: '单位',
		                    name: 'unit',
		                    anchor:'70%'
		                }]						
					},{
						columnWidth:.3,
						layout: 'form',
		                border:false,
						bodyStyle : 'background-color: transparent;',
						items: {
							id: 'imageview',
							xtype: 'panel',
							//iconCls: 'img-upload',
							bodyStyle: 'cursor:pointer;',
							height:100,width:150,
							border: true,
							listeners: {
								afterrender : function(obj,evt) {
									obj.getEl().dom.onclick = this.fileUploadHandler.createDelegate(this);
								}.createDelegate(this)
							}
						}							
					},{
		                columnWidth:.5,
		                layout: 'form',
		                border:false,
						bodyStyle : 'background-color: transparent;',
		                items: [ new Ext.form.ComboBox({
							tpl: comboxTpl,
							fieldLabel: '生产厂商',
					        store: DataStoreUtil.getManufacturersStore(),
					        displayField:'name',
					        typeAhead: true,
					        mode: 'local',
					        forceSelection: false,
					        triggerAction: 'all',
					        emptyText:'选择或输入生产厂商...',
					        selectOnFocus:true,
							name: 'manufacturer',
							anchor:'95%'
					    }), new Ext.form.ComboBox({
							tpl: comboxTpl,
							fieldLabel: '商品分类',
					        store: DataStoreUtil.getCategoriesStore(),
					        displayField:'name',
					        typeAhead: true,
					        mode: 'local',
					        forceSelection: false,
					        triggerAction: 'all',
					        emptyText:'选择或输入商品分类...',
					        selectOnFocus:true,
							name: 'category',
							anchor:'95%'
					    })]
		            },{
		                columnWidth:.5,
		                layout: 'form',
		                border:false,
						bodyStyle : 'background-color: transparent;',
						labelWidth: 35,
		                items: [{
		                	xtype:'trigger',
		                    fieldLabel: '图片',
		                    triggerClass: 'x-form-search-trigger',
		                    name: 'imageurl',
		                    anchor:'95%',
		                    onTriggerClick: this.fileUploadHandler.createDelegate(this)
		                },{
		                    xtype:'textfield',
		                    fieldLabel: '规格',
		                    name: 'specs',
		                    anchor:'95%'
		                },{
		                    xtype:'textfield',
		                    fieldLabel: '型号',
		                    name: 'model',
		                    anchor:'95%'
		                }]
		            }]
		        },{
		            xtype:'fieldset',
					title: '初始数据设定',
					collapsible: true,
					autoHeight:true,
		            anchor:'100%',
					layout:'column',
					
					items:[{
						columnWidth:.36,
						layout: 'form',
						labelWidth: 85,
		                border:false,
						bodyStyle : 'background-color: transparent;',
						items: [{
							xtype:'numberfield',
							fieldLabel: '预设售价',
	                		name: 'price',
	                		anchor:'95%'
						}]							
					},{
						columnWidth:.32,
						layout: 'form',
		                border:false,
						bodyStyle : 'background-color: transparent;',
						items: [{
							xtype:'numberfield',
							fieldLabel: '内部价格',
	                		name: 'base_price',
	                		anchor:'95%'
						}]						
					},{
						columnWidth:.32,
						layout: 'form',
		                border:false,
						bodyStyle : 'background-color: transparent;',
						items: [{
							xtype:'numberfield',
							fieldLabel: '采购价格',
	                		name: 'buy_price',
	                		anchor:'95%'
						}]						
					}]
		        }],
				
				buttons: [{
		            text: '保存',
					listeners: {
						"click": function(){
							this.syncJson();
							
							if(this.formJson.code && this.formJson.code != "") {
								this.saveProductInfo();
							}
							
						}.createDelegate(this)
					}
		        },{
		            text: '取消',
					listeners:{
						"click":this.cancelHandler.createDelegate(this)
					}
		        }]
			});
			
			this.items = this.inputForm;
			
			SALE.window.ProductsEditWindow.superclass.initComponent.call(this);
		},
		
		saveProductInfo : function() {
			SALE.Action.saveProductInfo(this.saveSuccessHandler.createDelegate(this), this.formJson, this.task);
		},
		
		cancelHandler : function() {
			this.clear();
			this.hide();
		},
		
		listeners: {
			'show': function() {
				this.syncForm();
				if(this.formJson && this.formJson.imageurl) {
					Ext.DomHelper.overwrite('imageview', {
			    		tag: 'img', src: this.formJson.imageurl, 
			    		style:'margin:0 auto;visibility:hidden;',
			    		width: 150, height: 100
			    	}, true).show(true).frame();
				} else {
					Ext.DomHelper.overwrite('imageview', {
			    		tag: 'img', src: '', 
			    		style:'margin:0 auto;visibility:hidden;',
			    		width: 150, height: 100
			    	}, true).show(true).frame();
				}
			},
			
			'hide': function() {							
				this.clear();
			}
		}
	});
}

SALE.window.showProductsEditWindow = function(successCallback, initJson, task) {
	if(!this.editWin) {
		this.editWin = new SALE.window.ProductsEditWindow();
	}
	this.editWin.showWindow(successCallback, initJson, task);
}